#include "widget.h"
#include "./ui_widget.h"

#include <QPaintEvent>
#include <QPainter>
#include <QRect>
#include <QBrush>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setPalette(QPalette(Qt::white)); // 设置窗口为白色
    setAutoFillBackground(true); // 调用paintEvent前自动填充背景
    this->resize(400, 300);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this); // 构造时指定画板
    painter.setRenderHint(QPainter::Antialiasing); // 抗锯齿
    painter.setRenderHint(QPainter::TextAntialiasing); // 字体抗锯齿

    // 绘图区尺寸
    int w = this->width();
    int h = this->height();

    QRect rect(w / 4, h / 4, w / 2, h / 2);

    // 设置画笔
    QPen pen;
    pen.setWidth(3);
    pen.setColor(Qt::red);
    pen.setStyle(Qt::SolidLine); // SolidLine: 实线; DashLine: 虚线
    pen.setCapStyle(Qt::FlatCap); // 扁平帽
    pen.setJoinStyle(Qt::BevelJoin); // 两条线的交界处: 根据两根线的角度
    painter.setPen(pen);

    // 设置画刷
    QBrush brush;
    brush.setColor(Qt::yellow);
    brush.setStyle(Qt::SolidPattern);
    painter.setBrush(brush);

    // 绘图
    painter.drawRect(rect);

    event->accept();
}
